import { Color } from 'https://unpkg.com/three/build/three.module.js';
import { initShaders } from '../jsm/Utils.js';
import lib from '../jsm/ThreeLib.js';

window.onload = function () {

    const backColor = new Color("rgba(0,0,0,1)");

    const canvas = document.getElementById("canvas");
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;

    const gl = canvas.getContext("webgl");
    //开启颜色合成,才会有透明度的感觉
    gl.enable(gl.BLEND);
    gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);

    //初始化着色器
    initShaders(gl,
        document.querySelector("#vertexShader").innerText,
        document.querySelector("#fragmentShader").innerText);


    var vertices = new Float32Array([
        0.0, 0.1,
        -0.1, -0.1,
        0.1, -0.1,
        0.2, 0.1,
    ]);
    //缓冲对象 
    var vertexBuffer = gl.createBuffer();
    //绑定缓冲对象
    gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
    //写入数据(缓冲区对象,顶点数据,写入方式[一次性写入])
    gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);


    var a_posi = gl.getAttribLocation(gl.program, 'a_Position');
    gl.vertexAttribPointer(a_posi, 2, gl.FLOAT, false, 0, 0);

    var a_PointSize = gl.getAttribLocation(gl.program, 'a_PointSize');
    gl.vertexAttrib1f(a_PointSize, 20);

    var u_FragColor = gl.getUniformLocation(gl.program, 'u_FragColor');
    gl.uniform4fv(u_FragColor, [1, 1, 0, 1]);

    //赋能-批处理
    gl.enableVertexAttribArray(a_posi);



    lib.clearCanvas(gl, backColor, 1);

    gl.drawArrays(gl.POINTS, 0, 4);





}